package com.stylefeng.guns.modular.system.task;

import java.net.URI;
import java.net.URL;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.IndustryRankMapper;
import com.stylefeng.guns.modular.system.model.IndustryRank;

@Service
public class IndustryImportTask {

	private Logger log = LoggerFactory.getLogger(IndustryImportTask.class);
	
	@Autowired
	private IndustryRankMapper industryRankMapper;
	
	private static final HttpClientUtil clientUtil = new HttpClientUtil();
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			String url = ImportContants.getIndustryDataPathStr(1);
			HttpGet httpGet = HttpClientUtil.getGetRequest();
			try {
				URL http = new URL(url);
				URI uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
				httpGet.setURI(uri);
				String json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
				JSONObject jsonObj = JSON.parseObject(json);
				if(jsonObj.getIntValue("pages") > 0){
					parseIndustryData(jsonObj.getString("data"));
					int pages = jsonObj.getIntValue("pages");
					for(int i= 2; i <= pages ; i++){
						url = ImportContants.getIndustryDataPathStr(i);
						http = new URL(url);
						uri = new URI(http.getProtocol(), http.getHost(), http.getPath(), http.getQuery(), null);
						httpGet.setURI(uri);
						json = clientUtil.executeAndGetWithFailedRetry(httpGet, 6,10001);
						jsonObj = JSON.parseObject(json);
						parseIndustryData(jsonObj.getString("data"));
					}
				}
			} catch (Exception e) {
				log.error("get industry data is error!" + e);
			}finally{
				httpGet.abort();
			}
		}
	}
	
	private void parseIndustryData(String json){
		String[] industrys = StringUtils.substringsBetween(json, "\"", "\"");
		for(String industry : industrys){
			try{
				if(StringUtils.isNotEmpty(industry)){
					String[] detail = StringUtils.split(industry,",");
					IndustryRank industryRank = new IndustryRank();
					industryRank.setgInstCode(detail[1]);
					industryRank.setgTime(new Date());
					industryRank = industryRankMapper.selectByDate(industryRank);
					if(industryRank != null){
						industryRank.setgName(detail[2]);
						industryRank.setgZdf(Double.valueOf(detail[3]));
						industryRank.setgZllr(Double.valueOf(detail[4]));
						industryRank.setgCdlr(Double.valueOf(detail[6]));
						industryRank.setgDdlr(Double.valueOf(detail[8]));
						industryRank.setgZdlr(Double.valueOf(detail[10]));
						industryRank.setgXdlr(Double.valueOf(detail[12]));
						industryRank.setgZllrGp(StringUtils.substring(detail[15], 0, 6));
						industryRankMapper.updateById(industryRank);
					}else{
						industryRank = new IndustryRank();
						industryRank.setgInstCode(detail[1]);
						industryRank.setgTime(new Date());
						industryRank.setgName(detail[2]);
						industryRank.setgZdf(Double.valueOf(detail[3]));
						industryRank.setgZllr(Double.valueOf(detail[4]));
						industryRank.setgCdlr(Double.valueOf(detail[6]));
						industryRank.setgDdlr(Double.valueOf(detail[8]));
						industryRank.setgZdlr(Double.valueOf(detail[10]));
						industryRank.setgXdlr(Double.valueOf(detail[12]));
						industryRank.setgZllrGp(StringUtils.substring(detail[15], 0, 6));
						industryRankMapper.insert(industryRank);
					}
				}
			}catch(Exception e){
				log.error("parse error!" ,e);
			}
		}
	}
}
